var BallScript = (function (_super) {

    function BallScript() {
        BallScript.super(this);
        this.ball = null;
        this.inPause = false;
        this.inEatCapsuleEff = false;
        this.reset();
    }
    Laya.class(BallScript, "BallScript", _super);

    BallScript.prototype.reset = function () {
        this.testPause = false;
        //下落上升
        this.DownSpeed = Config.FastestSpeed;
        this.yy = Config.BallStartYY;
        this.lowestYY = 0;              //  历史最低高度，当低于这个值的时候，移动摄像机
        this.jumpHeight = 0;            //弹球弹跳的高度
        this.speedChange = Config.GravitationalSpeed*GameSceneObj.timeInterval;
        //缩放
        this.scaleNum = Config.BallMaxScale;
        this.scaleChangeNum = Config.BallScaleChangeNum;
    }
    BallScript.prototype.resetBall = function () {
        this.reset();
        if(GameSceneObj._skinType == "wx"){

        }else{
            this.ball.transform.localScale = new Laya.Vector3(this.scaleNum, 4, this.scaleNum);
            this.ball.transform.localPosition =  new Laya.Vector3(0, this.yy, Config.BallStartZZ);
        }
        
    }
    /**
    * 脚本实例化完成载入后调度
    *  owner 脚本绑定的3D物体
    */
    BallScript.prototype._load = function (owner) {
        this.ball = owner;
    }

    /**
     * 脚本帧循环更新
     */
    BallScript.prototype._update = function (state) {
        if(GameSceneObj.isPause || this.testPause) return;
        //位置更新
        if(this.DownSpeed > Config.FastestSpeed){
            this.DownSpeed += this.speedChange;
            if(this.DownSpeed < Config.FastestSpeed){
                this.DownSpeed = Config.FastestSpeed;
            } 
        }
        var __yy = GameSceneObj.timeInterval * this.DownSpeed;
        if(this.inEatCapsuleEff || this.inPause){
            __yy = 0;
        }
        this.yy += __yy;
        
        // 放大缩小的动效
        if(this.scaleNum >= Config.BallMaxScale){
            this.scaleChangeNum = -Config.BallScaleChangeNum;
        }
        if(this.scaleNum <= Config.BallMinScale){
            this.scaleChangeNum = Config.BallScaleChangeNum;
        } 
        this.scaleNum += this.scaleChangeNum;
        this.ball.transform.localScale = new Laya.Vector3(this.scaleNum, this.scaleNum , Config.BallMinScale);

        this.ball.transform.localPosition = new Laya.Vector3(0, this.yy, Config.BallStartZZ);
        if(this.yy < this.lowestYY){
            this.lowestYY = this.yy;
            GlobalCamera.transform.localPosition = new Laya.Vector3(0,Config.GlobalCameraPositionYY + this.yy, Config.GlobalCameraPositionZZ);
            GameSceneObj.column.transform.position = new Laya.Vector3(0,Config.GlobalColumnPositionYY + this.yy,0);
            GameSceneObj.bgEff.transform.localPosition = new Laya.Vector3(0, this.yy-50, 30);
            
            // GameSceneObj.__zzFlag.transform.localPosition = new Laya.Vector3(0, this.yy, 30);
            // GameSceneObj.__xxFlag.transform.localPosition = new Laya.Vector3(30, this.yy, 0);
        }
        //Laya.timer.frameOnce(3, this, function () { this.bullet.destroy(); });
    }
    /**
     * 当其他碰撞器进入绑定物体碰撞器时触发（击中物品时）
     * 注：如相对移动速度过快，可能直接越过
     */
    BallScript.prototype.onTriggerEnter = function (other) {
        console.log("---------击中物品时--------------",other.type);
    }
    /**
     * 当其他碰撞器进入绑定物体碰撞器后逐帧触发（进入物品时）
     * 注：如相对移动速度过快，可能直接越过
     */
    BallScript.prototype.onTriggerStay = function (other) {
    }
    /**
     * 当其他碰撞器退出绑定物体碰撞器时逐帧触发（穿出物品时）
     * 注：如相对移动速度过快，可能直接越过
     */
    BallScript.prototype.onTriggerExit = function (other) {
        console.log("---------穿出物品时--------------");
    }

    BallScript.prototype.bounceBall = function () {
        
        var _sphere = null;
        var _collisin_eff = null;
        
        if(GameSceneObj.isInvincible){
            if(GlobalSphereRed){
                _sphere = GlobalSphereRed.clone();
            }
            _collisin_eff = GlobalCollisionEffRed.clone();
        }else{
            if(GameSceneObj.sphere){
                _sphere = GameSceneObj.sphere.clone();
            }
            _collisin_eff = GameSceneObj.collisionEff.clone();
        }
        
        var floorItem =  GameSceneObj.floorArr[GameSceneObj.nowFloorIndex];
        if(_sphere){
            _sphere.active = true;
            floorItem[0].__ground.addChild(_sphere);
            _sphere.transform.position = new Laya.Vector3(0,GameSceneObj.nowFloorIndexYY+6.5,-18);
            _sphere.transform.localScale = new Laya.Vector3(1.8,1.8,1.8);
            _sphere.transform.rotate(new Laya.Vector3(0,Math.random()*360,0), true, false);
            Laya.timer.frameOnce(600, this, function (_sphere) {
                GameSceneObj.__destroyDom(_sphere); 
            },[_sphere]);
        }
        _collisin_eff.active = true;
        
        if(GameSceneObj._skinType == "wx"){
            _collisin_eff.transform.localScale = new Laya.Vector3(1.8,1.8,1.8);
            _collisin_eff.transform.position = new Laya.Vector3(0,GameSceneObj.nowFloorIndexYY+8,-20);
            this.inPause = true;
            // Laya.timer.frameOnce(3, this, function () {
                this.DownSpeed = 100;
                this.inPause = false;
                Audio.playVoice("Audios/playerJump.wav",1);
                GlobalScene.addChild(_collisin_eff);
            // })
        }else{
            _collisin_eff.transform.localScale = new Laya.Vector3(1,1,1);
            _collisin_eff.transform.position = new Laya.Vector3(0,GameSceneObj.nowFloorIndexYY+3.2,-18);
            this.DownSpeed = 100;
            Audio.playVoice("Audios/playerJump.wav",1);
            GlobalScene.addChild(_collisin_eff);
        }
        Laya.timer.frameOnce(130, this, function (_collisin_eff) { 
            GameSceneObj.__destroyDom(_collisin_eff);
        },[_collisin_eff]);
        
    }
    BallScript.prototype.showEatCapsuleEff = function () {
        var _eat_capsule_eff = GlobalEatCapsuleEff.clone();
        this.inEatCapsuleEff = true;
        _eat_capsule_eff.transform.localScale = new Laya.Vector3(0.075,0.075,0.075);
        _eat_capsule_eff.transform.position = new Laya.Vector3(0,0.1,0);
        _eat_capsule_eff.active = true;
        this.ball.addChild(_eat_capsule_eff);
        Laya.timer.frameOnce(30, this, function (_eat_capsule_eff) { 
            GameSceneObj.__destroyDom(_eat_capsule_eff);
            this.inEatCapsuleEff = false;
            if(GameSceneObj._skinType == "wx"){
                this.showEatCapsuleEffWx();
            }
        },[_eat_capsule_eff]);
    }
    BallScript.prototype.showEatCapsuleEffWx = function () {
        var _capsule_eff_down = GlobalCapsuleEffDown.clone();
        _capsule_eff_down.transform.scale = new Laya.Vector3(0.1,0.09,0.125);
        _capsule_eff_down.transform.localPosition = new Laya.Vector3(0,-0.1,0);
        _capsule_eff_down.active = true;
        this.ball.addChild(_capsule_eff_down);

        var _capsule_eff_ball = GlobalCapsuleEffBall.clone();
        _capsule_eff_ball.transform.localPosition = new Laya.Vector3(0,0,0);
        _capsule_eff_ball.active = true;
        this.ball.addChild(_capsule_eff_ball);

        var _capsule_eff_bg = GlobalCapsuleEffBg.clone();
        _capsule_eff_bg.transform.localScale = new Laya.Vector3(1,1,1.5);
        _capsule_eff_bg.transform.localPosition = new Laya.Vector3(0,-3,3);
        _capsule_eff_bg.active = true;
        this.ball.addChild(_capsule_eff_bg);
        Laya.timer.frameOnce(190, this, function (_capsule_eff_down,_capsule_eff_ball,_capsule_eff_bg) { 
            GameSceneObj.__destroyDom(_capsule_eff_down);
            GameSceneObj.__destroyDom(_capsule_eff_ball);
            GameSceneObj.__destroyDom(_capsule_eff_bg);
        },[_capsule_eff_down,_capsule_eff_ball,_capsule_eff_bg]);
    }
    return BallScript;
})(Laya.Script);